package ar.republicagroup.gestion.view;

import ar.republicagroup.gestion.entities.BolsaCheque;
import ar.republicagroup.gestion.entities.Cheque;
import ar.republicagroup.gestion.entities.ChequeEstado;
import ar.republicagroup.gestion.entities.ChequeTipo;
import ar.republicagroup.gestion.entities.Cliente;
import ar.republicagroup.gestion.entities.CuentaBanco;
import ar.republicagroup.gestion.services.BolsaChequeServices;
import ar.republicagroup.gestion.services.CalendarioBancarioServices;
import ar.republicagroup.gestion.services.ChequeEstadoServices;
import ar.republicagroup.gestion.services.ChequeServices;
import ar.republicagroup.gestion.services.ChequeTipoServices;
import ar.republicagroup.gestion.services.ClienteServices;
import ar.republicagroup.gestion.services.CuentaBancoServices;
import ar.republicagroup.gestion.services.LiquidacionServices;
import ar.republicagroup.gestion.util.JComboBoxModel;
import ar.republicagroup.gestion.util.SpringUtils;
import ar.republicagroup.gestion.util.TableColumnAdjuster;
import ar.republicagroup.gestion.util.UserSession;
import ar.republicagroup.gestion.util.Validar;
import ar.republicagroup.gestion.util.helpers.Formato;
import ar.republicagroup.gestion.util.helpers.UserInterfaceHelper;
import ar.republicagroup.gestion.util.messages.UIMessages;
import java.awt.HeadlessException;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;

public class frmCheque extends javax.swing.JFrame {

    private static Logger log                   = Logger.getLogger(frmCheque.class);
    
    /* Services */
    ChequeServices  chequeServices                  = (ChequeServices)SpringUtils.getBean("chequeServices");
    BolsaChequeServices bolsaChequeServices         = (BolsaChequeServices)SpringUtils.getBean("bolsaChequeServices");
    ClienteServices clienteServices                 = (ClienteServices)SpringUtils.getBean("clienteServices");
    ChequeEstadoServices chequeEstadoServices       = (ChequeEstadoServices)SpringUtils.getBean("chequeEstadoServices"); 
    LiquidacionServices liquidacionServices         = (LiquidacionServices)SpringUtils.getBean("liquidacionServices");
    CuentaBancoServices cuentaBancoServices         = (CuentaBancoServices)SpringUtils.getBean("cuentaBancoServices");
    ChequeTipoServices chequeTipoServices           = (ChequeTipoServices)SpringUtils.getBean("chequeTipoServices");
    CalendarioBancarioServices calendarioServices   = (CalendarioBancarioServices)SpringUtils.getBean("calendarioBancarioServices");
    
    /* Listados*/
    List<Cheque>    chequeList                         = new ArrayList<>();
    static List<BolsaCheque> bolsaChequeList           = new ArrayList<>();
    
    /* Beans */
    static Cheque chequeToEdit;
    
    /*Constantes*/
    public static final int LISTADO_CHEQUE = 1;
    
    private static int selectedBolsa = -1;
    
    public frmCheque() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/moneybox.png")).getImage());
        valoresPorDefecto();
        cargarDatos();
    }
    
    private void cargarDatos(){
            comboClientes.setModel(clienteServices.combo());
            comboChequeEstado.setModel(chequeEstadoServices.combo(LISTADO_CHEQUE));
            comboCuentas.setModel(cuentaBancoServices.combo());
            comboTipoCheque.setModel(chequeTipoServices.combo());
            comboEstadosCustom.setModel(chequeEstadoServices.combo(ChequeEstadoServices.ESTADOS_CUSTOMIZABLES));
            bolsaChequeList = bolsaChequeServices.findEntities();
            cargarTablaBolsaCheque(bolsaChequeList);
    }
    
    public static void cargarTablaCheque(List<Cheque> listado){
        int     totalChequesVC = 0;
        int     totalChequesCAP = 0;
        int     totalCheques = 0;
        double  subtotalVC   = 0;
        double  subtotalCAP  = 0;
       
        DefaultTableModel model = (DefaultTableModel) tblCheque.getModel();
        model.getDataVector().clear();
        tblCheque.clearSelection();
       
        for(Cheque cheque : listado){
          model.addRow(new Object[]{cheque.getBolsaChequeId().getId(),cheque.getChequeTipoId().getTipo(),cheque,Formato.fechaDDMMYY(cheque.getFechaAlta()),cheque.getBolsaChequeId().getClienteId().getNombre(),cheque.getBancoId().getNombre(),cheque.getCodigoCtaBancaria(),cheque.getCuentaBancoId().getNombre(),cheque.getOrigen(),Formato.decimalComaMilesConSigno(cheque.getImporte().floatValue()),cheque.getComision().floatValue(),cheque.getBolsaChequeId().getUsuarioId().getNombre(),
                new JComboBoxModel(cheque.getChequeEstadoId().getId().toString(), cheque.getChequeEstadoId().getNombre())});
            
            
            if(cheque.getOrigen().equals(ChequeServices.ORIGEN_CAP_BSAS)){
                totalChequesCAP++;
                subtotalCAP += cheque.getImporte().doubleValue();
            }    
            
            if(cheque.getOrigen().equals(ChequeServices.ORIGEN_VC)){
                totalChequesVC++;
                subtotalVC += cheque.getImporte().doubleValue();
            } 
            
            totalCheques++;
        }
        
        
        lblTotalChequesVC.setText(""+totalChequesVC);
        lblTotalChequesCAP.setText(""+totalChequesCAP);
        lblTotalChequesEncontrados.setText(""+totalCheques);
        lblSubtotalCAP.setText(""+Formato.decimalComaMilesConSigno(subtotalCAP));
        lblSubtotalVC.setText(""+Formato.decimalComaMilesConSigno(subtotalVC));
        lblTotalImporteCheques.setText(Formato.decimalComaMilesConSigno(subtotalVC + subtotalCAP));
        TableColumnAdjuster tca = new TableColumnAdjuster(tblCheque);
        tca.adjustColumns();
         
        tblCheque.setAutoCreateRowSorter(false);
        tblCheque.setModel(model);
        tblCheque.repaint();
    
    }
    
    public static void cargarTablaBolsaCheque(List<BolsaCheque> listado){
        DateFormat formatDate = DateFormat.getDateInstance();
        
        DefaultTableModel model = (DefaultTableModel) tblBolsaCheque.getModel();
        model.getDataVector().clear();
        
        for(BolsaCheque bolsaCheque : listado){
            model.addRow(new Object[]{ bolsaCheque,formatDate.format(bolsaCheque.getFechaAlta()),Formato.obtenerFechaYHoraDeDate(bolsaCheque.getFechaAlta()),bolsaCheque.getClienteId().getNombre(),bolsaCheque.getUsuarioId().getNombre(),});
        }
        
        TableColumnAdjuster tca = new TableColumnAdjuster(tblBolsaCheque);
        tca.adjustColumns();
        tblBolsaCheque.setModel(model);
        
        if(selectedBolsa != -1){
            ListSelectionModel selection = tblBolsaCheque.getSelectionModel();
            selection.addSelectionInterval(selectedBolsa, selectedBolsa);
            tblBolsaCheque.setSelectionModel(selection);
        }
        
        tblBolsaCheque.repaint();
    }
    
    public static void cargaTablaAfterEdit(){
       cargarTablaBolsaCheque(bolsaChequeList);
       cargarTablaCheque(bolsaChequeList.get(selectedBolsa).getChequeList());   
    }
    
    
 
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupVer = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCheque = new javax.swing.JTable();
        panelCrud = new javax.swing.JPanel();
        btnNuevoCheque = new javax.swing.JButton();
        btnActualizar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnCambioCliente = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblBolsaCheque = new javax.swing.JTable();
        btnVerCheques = new javax.swing.JButton();
        btnModificarBolsa = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lblTotalChequesEncontrados = new javax.swing.JLabel();
        lblTotalImporteCheques = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblTotalChequesVC = new javax.swing.JLabel();
        lblTotalChequesCAP = new javax.swing.JLabel();
        lblSubtotalCAP = new javax.swing.JLabel();
        lblSubtotalVC = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        btnAplicar = new javax.swing.JButton();
        comboCuentas = new javax.swing.JComboBox();
        jPanel7 = new javax.swing.JPanel();
        btnCambiarEstado = new javax.swing.JButton();
        comboEstadosCustom = new javax.swing.JComboBox();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel8 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        calendarDesde = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        calendarHasta = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        comboClientes = new javax.swing.JComboBox();
        btnBuscarBolsa = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        comboBusqueda = new javax.swing.JComboBox();
        txtBusqueda = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnFiltrarCheques = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        comboTipoCheque = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        comboChequeEstado = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        calendarDesde1 = new com.toedter.calendar.JDateChooser();
        jLabel14 = new javax.swing.JLabel();
        calendarDesde2 = new com.toedter.calendar.JDateChooser();
        jLabel15 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("...: Gestión de cheques :...");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        tblCheque.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Bolsa", "Tipo", "Cheque.Nro", "Fecha alta", "Cliente", "Banco", "Cta. Bancaria", "Cta. Destino", "Origen", "Importe", "Com", "Usuario", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCheque.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblCheque.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(tblCheque);

        panelCrud.setBorder(javax.swing.BorderFactory.createTitledBorder("Operaciones con cheques"));
        panelCrud.setToolTipText("");

        btnNuevoCheque.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/add.png"))); // NOI18N
        btnNuevoCheque.setText("Nuevo");
        btnNuevoCheque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoChequeActionPerformed(evt);
            }
        });

        btnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/update.png"))); // NOI18N
        btnActualizar.setText("Actualizar");
        btnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActualizarActionPerformed(evt);
            }
        });

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/edit.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/printer.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        btnCambioCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/account_balances.png"))); // NOI18N
        btnCambioCliente.setText("Cambio de cliente");
        btnCambioCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambioClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelCrudLayout = new javax.swing.GroupLayout(panelCrud);
        panelCrud.setLayout(panelCrudLayout);
        panelCrudLayout.setHorizontalGroup(
            panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCrudLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCambioCliente)
                    .addComponent(btnNuevoCheque))
                .addGap(18, 18, 18)
                .addGroup(panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnActualizar)
                    .addComponent(btnModificar))
                .addGap(18, 18, 18)
                .addGroup(panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEliminar)
                    .addComponent(btnImprimir))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        panelCrudLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnActualizar, btnCambioCliente, btnEliminar, btnImprimir, btnModificar, btnNuevoCheque});

        panelCrudLayout.setVerticalGroup(
            panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCrudLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevoCheque)
                    .addComponent(btnActualizar)
                    .addComponent(btnImprimir))
                .addGap(18, 18, 18)
                .addGroup(panelCrudLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCambioCliente)
                    .addComponent(btnModificar)
                    .addComponent(btnEliminar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Bolsa de cheques"));

        tblBolsaCheque.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Bolsa", "Fecha", "Hora", "Cliente", "Usuario"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblBolsaCheque);

        btnVerCheques.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/search_binoculars.png"))); // NOI18N
        btnVerCheques.setText("Ver");
        btnVerCheques.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerChequesActionPerformed(evt);
            }
        });

        btnModificarBolsa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/edit.png"))); // NOI18N
        btnModificarBolsa.setText("Modificar");
        btnModificarBolsa.setEnabled(false);
        btnModificarBolsa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarBolsaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnModificarBolsa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnVerCheques, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnVerCheques)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificarBolsa)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));

        jLabel7.setText("Cheques :");

        jLabel8.setText("Total importe:");

        lblTotalChequesEncontrados.setText("0");

        lblTotalImporteCheques.setText("0");

        jLabel5.setText("Cheques VC:");

        jLabel9.setText("Cheques CAP:");

        jLabel10.setText("Subtotal VC:");

        jLabel11.setText("Subtotal CAP:");

        lblTotalChequesVC.setText("0");

        lblTotalChequesCAP.setText("0");

        lblSubtotalCAP.setText("0");

        lblSubtotalVC.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTotalChequesEncontrados))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblTotalChequesVC))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(lblTotalChequesCAP)))
                .addGap(32, 32, 32)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTotalImporteCheques)
                    .addComponent(lblSubtotalCAP)
                    .addComponent(lblSubtotalVC))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(lblTotalChequesEncontrados)
                    .addComponent(jLabel10)
                    .addComponent(lblSubtotalVC))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel11)
                    .addComponent(lblTotalChequesVC)
                    .addComponent(lblSubtotalCAP))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(lblTotalImporteCheques)
                    .addComponent(lblTotalChequesCAP))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAplicar.setText("Aplicar");
        btnAplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAplicarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnAplicar, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(comboCuentas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(86, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboCuentas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAplicar)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Asignar cuenta", jPanel4);

        btnCambiarEstado.setText("Aplicar");
        btnCambiarEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarEstadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboEstadosCustom, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCambiarEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(85, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboEstadosCustom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCambiarEstado)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Cambiar estado", jPanel7);

        jLabel1.setText("Desde");

        Locale locale = new Locale("es", "MX");
        calendarDesde.setLocale(locale);
        calendarDesde.setDateFormatString("dd/MM/yyyy");
        calendarDesde.setName("Calendario"); // NOI18N

        jLabel3.setText("Hasta");

        Locale locale2 = new Locale("es", "MX");
        calendarDesde.setLocale(locale2);

        jLabel2.setText("Cliente");

        btnBuscarBolsa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/search_2.png"))); // NOI18N
        btnBuscarBolsa.setText("Buscar ");
        btnBuscarBolsa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarBolsaActionPerformed(evt);
            }
        });

        jLabel12.setText("Número");

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(calendarHasta, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(calendarDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscarBolsa, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        jPanel8Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {calendarDesde, calendarHasta});

        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calendarDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calendarHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(btnBuscarBolsa)
                .addContainerGap(70, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Buscar bolsas de cheques", jPanel8);

        comboBusqueda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Por número", "Por importe" }));

        txtBusqueda.setText("Ingrese datos");
        txtBusqueda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtBusquedaFocusGained(evt);
            }
        });
        txtBusqueda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBusquedaKeyPressed(evt);
            }
        });

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ar/republicagroup/gestion/view/images/search_2.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnFiltrarCheques.setText("Filtrar");
        btnFiltrarCheques.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiltrarChequesActionPerformed(evt);
            }
        });

        jLabel13.setText("Origen");

        jLabel6.setText("Tipo de cheque");

        jLabel4.setText("Estado");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "VC", "CAP" }));
        jComboBox1.setEnabled(false);

        Locale locale3 = new Locale("es", "MX");
        calendarDesde.setLocale(locale);
        calendarDesde1.setDateFormatString("dd/MM/yyyy");
        calendarDesde1.setName("Calendario"); // NOI18N

        jLabel14.setText("Desde");

        Locale locale4 = new Locale("es", "MX");
        calendarDesde.setLocale(locale);
        calendarDesde2.setDateFormatString("dd/MM/yyyy");
        calendarDesde2.setName("Calendario"); // NOI18N

        jLabel15.setText("Hasta");

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnFiltrarCheques)
                .addGap(0, 78, Short.MAX_VALUE))
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(comboBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(comboChequeEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCheckBox3))
                            .addComponent(txtBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14)
                            .addComponent(jLabel15))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(calendarDesde1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(comboTipoCheque, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBox1)
                                    .addComponent(jCheckBox2)))
                            .addComponent(calendarDesde2, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel9Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {comboChequeEstado, comboTipoCheque, jComboBox1});

        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel14)
                    .addComponent(calendarDesde1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(calendarDesde2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboTipoCheque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jCheckBox2))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboChequeEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jCheckBox3))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(btnFiltrarCheques))
                .addGap(34, 34, 34))
        );

        jTabbedPane2.addTab("Buscar cheques", jPanel9);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(92, 92, 92))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(panelCrud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 312, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTabbedPane1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelCrud, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
            if(tblCheque.getSelectedRow() != -1){
                        DefaultTableModel model = (DefaultTableModel)tblCheque.getModel();
                        Cheque cheque             = (Cheque)model.getValueAt(tblCheque.getSelectedRow(), 2);
                        int respuesta = JOptionPane.showConfirmDialog(this, "¿Desea eliminar el cheque "+cheque.getChequeNro()+" ?", "Eliminar cheque", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if(respuesta == 0){
                        String mensaje =  chequeServices.delete(cheque.getId());
                        JOptionPane.showMessageDialog(this, mensaje, "Cheque eliminado", JOptionPane.INFORMATION_MESSAGE);
                        chequeList.remove(cheque);    
                        cargarTablaCheque(chequeList);
                        }  
            } else {
                JOptionPane.showMessageDialog(this, "Debe seleccionar un cheque", "Error", JOptionPane.ERROR_MESSAGE);
            }   
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
         if(tblCheque.getSelectedRow() != -1){
             if(tblCheque.getSelectedRows().length == 1){
             System.out.println("tblCheque.getSelectedRows().length: "+tblCheque.getSelectedRows().length);
             DefaultTableModel model    = (DefaultTableModel)tblCheque.getModel();
             chequeToEdit             = (Cheque)model.getValueAt(tblCheque.getSelectedRow(), 2);
                if(chequeToEdit.getFechaLiquidado() == null){
                   modalPanelEditCheque panelEdit = new modalPanelEditCheque(this, true);
                   panelEdit.setLocationRelativeTo(this);
                   panelEdit.setChequeToUpdate(chequeToEdit);
                   panelEdit.setVisible(true);
                   if(panelEdit.isEditCompleted()){
                       if(frmLiquidacion.getInstance().isShowing()){
                           frmLiquidacion.getInstance().btnBuscar.doClick();
                       }
                   }

                } else {
                       JOptionPane.showMessageDialog(this, UIMessages.getInfoMessage("chequeLiquidado"), UIMessages.getInfoMessage("defaultInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
                }
          
             } else {
                       JOptionPane.showMessageDialog(this, UIMessages.getInfoMessage("justOneChequeSelected"), UIMessages.getInfoMessage("defaultInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
             }
          
          } else {
             JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCheque"), UIMessages.getInfoMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
          }   
   
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnNuevoChequeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoChequeActionPerformed
          frmAddCheque formulario = new frmAddCheque();
          formulario.setLocationRelativeTo(this);
          formulario.setVisible(true);
        
    }//GEN-LAST:event_btnNuevoChequeActionPerformed

    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed
         if(chequeToEdit != null){
            chequeList.clear();
            chequeList.add(chequeServices.get(chequeToEdit.getId()));
            cargarTablaCheque(chequeList);
            chequeToEdit = null;
         } else {
             bolsaChequeList = bolsaChequeServices.findEntities();
             cargarTablaBolsaCheque(bolsaChequeList);  
         }        
    }//GEN-LAST:event_btnActualizarActionPerformed

    private void btnVerChequesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerChequesActionPerformed
        mostrarCheques();
    }//GEN-LAST:event_btnVerChequesActionPerformed

    private void btnBuscarBolsaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarBolsaActionPerformed
            JComboBoxModel model    =(JComboBoxModel)comboClientes.getSelectedItem();
            Cliente cliente         = clienteServices.get(Integer.parseInt(model.getValue()));
            ChequeEstado chequeEstado = null;
            bolsaChequeList = bolsaChequeServices.getBolsaChequeByFilters(cliente,calendarDesde.getDate(),calendarHasta.getDate(),chequeEstado, false,null);
            cargarTablaBolsaCheque(bolsaChequeList);
    }//GEN-LAST:event_btnBuscarBolsaActionPerformed

    private void txtBusquedaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBusquedaFocusGained
        txtBusqueda.selectAll();
    }//GEN-LAST:event_txtBusquedaFocusGained

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        buscarCheques();
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnAplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAplicarActionPerformed
        int indices[] = tblCheque.getSelectedRows();
        JComboBoxModel comboModel = (JComboBoxModel)comboCuentas.getSelectedItem();
        
        if(indices.length > 0){
            DefaultTableModel model = (DefaultTableModel)tblCheque.getModel();
            List<Cheque> chequesToUpdateList = new ArrayList<>();
            List<Cheque> chequesLiquidadosList   = new ArrayList<>();
            
            for (int i=0; i < indices.length;i++){
                Cheque cheque = (Cheque)model.getValueAt(indices[i], 2);
                chequesToUpdateList.add(cheque);
            }
            
            for(Cheque cheque : chequesToUpdateList){
                CuentaBanco cuenta = cuentaBancoServices.get(Integer.valueOf(comboModel.getValue()));
                if(cheque.getFechaLiquidado() == null){
                    cheque.setCuentaBancoId(cuenta);
                    chequeServices.update(cheque);
                } else {
                    chequesLiquidadosList.add(cheque);
                }
            }
        
            if(!chequesLiquidadosList.isEmpty()){
                StringBuilder mensajeDeAdvertencia = new StringBuilder();
                mensajeDeAdvertencia.append("Los siguientes cheques : ");
                for(Cheque cheque : chequesLiquidadosList){
                    mensajeDeAdvertencia.append("\n").append(cheque.getChequeNro()).append(" / ").append(cheque.getBancoId().getNombre());
                    }
                mensajeDeAdvertencia.append("\n Ya se encuentran liquidados y no se les permite cambiar de cuenta");
                JOptionPane.showMessageDialog(this, mensajeDeAdvertencia,UIMessages.getInfoMessage("defaultInfoTitle"),JOptionPane.INFORMATION_MESSAGE);
            }
            
            model.fireTableDataChanged();  
            cargarTablaCheque(chequeList);
            frmMain.btnActualizar.doClick();
        }else {
            JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCheque"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnAplicarActionPerformed

    private void txtBusquedaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBusquedaKeyPressed
       if(evt.getKeyCode() == KeyEvent.VK_ENTER){
        buscarCheques();
       }
    }//GEN-LAST:event_txtBusquedaKeyPressed

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
        if(!chequeList.isEmpty()){
        try {
                MessageFormat header = new MessageFormat("Cheques",new Locale("es","ES"));
                boolean imprimio = tblCheque.print(JTable.PrintMode.FIT_WIDTH, header, null, true, null, true);
                if(imprimio){
                    log.info("Imprimió reporte de Clientes...");
                }
            } catch (PrinterException ex) {
                    log.error("Error al imprimir...");
                    log.error(ex.getMessage());
                } catch (HeadlessException ex) {
                    log.error(ex.getMessage());
                    log.error("Sin encabezado ...");
            }
        }
    }//GEN-LAST:event_btnImprimirActionPerformed

    private void btnCambiarEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarEstadoActionPerformed
        int indices[]                       = tblCheque.getSelectedRows();
        JComboBoxModel comboEstado          = (JComboBoxModel)comboEstadosCustom.getSelectedItem();
        DefaultTableModel tablaModel        = (DefaultTableModel)tblCheque.getModel();
        ChequeEstado nuevoChequeEstado      = chequeEstadoServices.get(Integer.parseInt(comboEstado.getValue()));
        
        if(indices.length > 0){
            List<Cheque> chequesToUpdateList = new ArrayList<>();
            List<Cheque> chequesLiquidadosList   = new ArrayList<>();
            for (int i=0; i < indices.length;i++){
                 Cheque cheque = (Cheque)tablaModel.getValueAt(indices[i], 2);
                 chequesToUpdateList.add(cheque);
            }
        
            /* Tranquilamente en Bussiness logic */
            for(Cheque cheque : chequesToUpdateList){
                if(cheque.getFechaLiquidado() == null){
                    if(nuevoChequeEstado.getId() == ChequeEstadoServices.CHEQUE_INGRESADO){
                        cheque.setFechaPendiente(null);
                    
                        if(cheque.getChequeEstadoId().getId() == ChequeEstadoServices.CHEQUE_RECHAZADO_1 ||
                           cheque.getChequeEstadoId().getId() == ChequeEstadoServices.CHEQUE_RECHAZADO_2 ||
                           cheque.getChequeEstadoId().getId() == ChequeEstadoServices.CHEQUE_RECHAZADO_3){
                           cheque.setFechaUpdate(null); 
                        } 
                        
                    }else{    
                        cheque.setFechaUpdate(new Date());
                    }
                    
   
                    cheque.setChequeEstadoId(nuevoChequeEstado);
                    chequeServices.update(cheque);
                } else {
                    chequesLiquidadosList.add(cheque);
                }
            }
            
            if(!chequesLiquidadosList.isEmpty()){
                StringBuilder mensajeDeAdvertencia = new StringBuilder();
                mensajeDeAdvertencia.append("Los siguientes cheques : ");
                for(Cheque cheque : chequesLiquidadosList){
                    mensajeDeAdvertencia.append("\n").append(cheque.getChequeNro()).append(" / ").append(cheque.getBancoId().getNombre());
                    }
                mensajeDeAdvertencia.append("\n Ya se encuentran liquidados y no se les permite cambiar de estado");
                JOptionPane.showMessageDialog(this, mensajeDeAdvertencia,UIMessages.getInfoMessage("defaultInfoTitle"),JOptionPane.INFORMATION_MESSAGE);
            }
            
             cargarTablaCheque(chequeList);
             frmMain.btnActualizar.doClick();
        }else{
           JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedCheque"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);  
        }    
    }//GEN-LAST:event_btnCambiarEstadoActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        frmMain.btnActualizar.doClick();
    }//GEN-LAST:event_formWindowClosed

    private void btnModificarBolsaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarBolsaActionPerformed
        selectedBolsa = tblBolsaCheque.getSelectedRow();

        if (selectedBolsa != -1){
            DefaultTableModel model = (DefaultTableModel)tblBolsaCheque.getModel();
            BolsaCheque bolsaCheque = (BolsaCheque)model.getValueAt(selectedBolsa, 0);
            if(bolsaChequeServices.isBolsaChequeAvailableToEdit(bolsaCheque)){
                modalPanelEditBolsaCheque panelEdit = new modalPanelEditBolsaCheque(this, true);
                UserInterfaceHelper.centreWindow(panelEdit);
                panelEdit.setBolsaCheque(bolsaCheque);
                panelEdit.setVisible(true);
            } else {
                JOptionPane.showMessageDialog(this, UIMessages.getInfoMessage("isBolsaAvailableToEdit"), UIMessages.getInfoMessage("defaultInfoTitle"), JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnModificarBolsaActionPerformed

    private void btnCambioClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambioClienteActionPerformed
            if(tblCheque.getSelectedRow() != -1){
                   int selectedRows[] = tblCheque.getSelectedRows();
                   List<Cheque> chequeList = new ArrayList<>();
                   
                   
                   for (int i = 0; i < selectedRows.length; i++) {
                     Cheque cheque = (Cheque)tblCheque.getValueAt(selectedRows[i], 2);
                     chequeList.add(cheque);
                   }
                   
                   modalPanelEditBolsaAndCheque panelEdit = new modalPanelEditBolsaAndCheque(this, true);
                   panelEdit.setChequeList(chequeList);
                   UserInterfaceHelper.centreWindow(panelEdit);
                   panelEdit.setVisible(true);
            }
    }//GEN-LAST:event_btnCambioClienteActionPerformed

    private void btnFiltrarChequesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiltrarChequesActionPerformed
        JComboBoxModel tipoDeChequeModel    = (JComboBoxModel)comboTipoCheque.getSelectedItem();
        JComboBoxModel estadoModel          = (JComboBoxModel)comboChequeEstado.getSelectedItem();
        ChequeTipo   chequeTipo             = chequeTipoServices.get(Integer.parseInt(tipoDeChequeModel.getValue()));
        ChequeEstado chequeEstado           = null;

        if(!(estadoModel.getLabel().equals("Todos"))){
            chequeEstado = chequeEstadoServices.get(Integer.parseInt(estadoModel.getValue()));
        }

        chequeList = chequeServices.getChequesByFilters(chequeTipo, chequeEstado,null,null,null,0);
        cargarTablaCheque(chequeList);
    }//GEN-LAST:event_btnFiltrarChequesActionPerformed

    public void mostrarCheques(){
        int fila = tblBolsaCheque.getSelectedRow();
        if(fila != -1)
            {    
                DefaultTableModel model = (DefaultTableModel)tblBolsaCheque.getModel();
                BolsaCheque bolsaCheque = (BolsaCheque)model.getValueAt(fila, 0);
                chequeList              = chequeServices.getChequesByBolsaId(bolsaCheque.getId());
                cargarTablaCheque(chequeList);
            }   
            else
                 {
                    JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredSelectedBolsaCheque"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
                 }
    }
    
    private void buscarCheques(){
        if(!Validar.esVacio(txtBusqueda.getText()))
        {
           if(comboBusqueda.getSelectedIndex() == 0 )
            { 
                chequeList =  chequeServices.getChequesByNumeroDeCheque(txtBusqueda.getText());
            } else 
                   {
                    chequeList = chequeServices.getChequesByImporte(Double.valueOf(txtBusqueda.getText()));
                    }
                    cargarTablaCheque(chequeList);
           
            if(chequeList.isEmpty())
            {
               JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("noChequeResults"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, UIMessages.getErrorMessage("requiredInputSearch"), UIMessages.getErrorMessage("defaultErrorTitle"), JOptionPane.ERROR_MESSAGE);
            txtBusqueda.requestFocus();
        }

    }
 
    
    public void reiniciarTextoDeBusqueda(JTextField caja,String mensaje){
            caja.setText(mensaje);
            caja.requestFocus();
            caja.selectAll(); 
    }
    
    private void valoresPorDefecto(){
        Locale local = new Locale("es", "US");
        calendarDesde.setLocale(local);
        calendarHasta.setLocale(local);
        calendarDesde.setDate(new Date());
        calendarHasta.setDate(new Date());
        calendarDesde.setDateFormatString("dd/MM/yyyy");
        calendarHasta.setDateFormatString("dd/MM/yyyy");
        
        if(UserSession.getInstance().getPerfil_id() == UserSession.ADMINISTRADOR){
            btnEliminar.setEnabled(true);
            btnModificar.setEnabled(true);
            btnModificarBolsa.setEnabled(true);
        }
        
    }
    
    
    
    
    public static void main(String args[])  {
       
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmCheque().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected static javax.swing.JButton btnActualizar;
    private javax.swing.JButton btnAplicar;
    private javax.swing.JButton btnBuscar;
    protected static javax.swing.JButton btnBuscarBolsa;
    private javax.swing.JButton btnCambiarEstado;
    private javax.swing.JButton btnCambioCliente;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnFiltrarCheques;
    private javax.swing.ButtonGroup btnGroupVer;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnModificarBolsa;
    private javax.swing.JButton btnNuevoCheque;
    private javax.swing.JButton btnVerCheques;
    private com.toedter.calendar.JDateChooser calendarDesde;
    private com.toedter.calendar.JDateChooser calendarDesde1;
    private com.toedter.calendar.JDateChooser calendarDesde2;
    private com.toedter.calendar.JDateChooser calendarHasta;
    private javax.swing.JComboBox comboBusqueda;
    private javax.swing.JComboBox comboChequeEstado;
    private javax.swing.JComboBox comboClientes;
    private javax.swing.JComboBox comboCuentas;
    private javax.swing.JComboBox comboEstadosCustom;
    private javax.swing.JComboBox comboTipoCheque;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jTextField1;
    private static javax.swing.JLabel lblSubtotalCAP;
    private static javax.swing.JLabel lblSubtotalVC;
    private static javax.swing.JLabel lblTotalChequesCAP;
    private static javax.swing.JLabel lblTotalChequesEncontrados;
    private static javax.swing.JLabel lblTotalChequesVC;
    private static javax.swing.JLabel lblTotalImporteCheques;
    private javax.swing.JPanel panelCrud;
    private static javax.swing.JTable tblBolsaCheque;
    protected static javax.swing.JTable tblCheque;
    private javax.swing.JTextField txtBusqueda;
    // End of variables declaration//GEN-END:variables
}
